package com.byd.sealmanage.web;

import com.alibaba.fastjson.JSONObject;
import com.byd.sealmanage.config.CommonResultOutput;
import com.byd.sealmanage.modal.bo.EngraveApplyParamVo;
import com.byd.sealmanage.modal.bo.SearchUserVO;
import com.byd.sealmanage.modal.po.TbUser;
import com.byd.sealmanage.service.DivisionSealService;
import com.byd.sealmanage.service.ExportExcelService;
import com.byd.sealmanage.util.JsonUtil;
import com.byd.sealmanage.util.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/divisionSeal")
public class DivisionSealController {

    @Autowired
    private ExportExcelService exportExcelService;

    @Autowired
    private DivisionSealService divisionSealService;

    /**
     * 测试过期时间
     * @param map
     * @return
     */
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public String test(@RequestBody Map<String,String> map){
        log.info("接收到的参数{}",map.get("param"));
        return map.get("param");
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadSealPicture",method = RequestMethod.POST)
    public JSONObject uploadSealPicture(@RequestParam("file") MultipartFile file){
        return exportExcelService.uploadPicture(file);
    }

    /**
     * 根据工号或姓名查询人员
     * @return
     */
    @RequestMapping(value = "peopleSearch",method = RequestMethod.GET)
    public JSONObject peopleSearch(SearchUserVO searchUserVO){
        return divisionSealService.getPeopleInfo(searchUserVO);
    }

    /**
     * 刻制申请提交
     * @param map
     * @return
     */
    @RequestMapping(value = "/engraveApply",method = RequestMethod.POST)
    public JSONObject engraveApply(@RequestBody Map<String,String> map){
        JSONObject jsonObject;
        try{
            jsonObject = divisionSealService.engraveApplySubmit(map);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput("9999","申请失败");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }

    /**
     * 获取事业部印章刻制信息
     * @return
     */
    @RequestMapping(value = "/getEngraveTable",method = RequestMethod.GET)
    public JSONObject getEngraveTable(EngraveApplyParamVo engraveApplyParamVo){
        TbUser userEntity = ShiroUtils.getUserEntity();
        return divisionSealService.getDivisionEngraveData(engraveApplyParamVo,userEntity);
    }

    /**
     * 印章备案
     * @return
     */
    @RequestMapping(value = "/sealFiling",method = RequestMethod.POST)
    public JSONObject sealFiling(@RequestBody Map<String,String> map){
        JSONObject jsonObject;
        try{
            jsonObject = divisionSealService.sealFilingService(map);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput("9999","印章备案失败");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }

    /**
     * 实体印章数据展示
     * @return
     */
    @RequestMapping(value = "/getEntitySealData",method = RequestMethod.GET)
    public JSONObject getEntitySealData(EngraveApplyParamVo engraveApplyParamVo){
        return divisionSealService.getEntitySealService(engraveApplyParamVo);
    }

    /**
     * 上传私自刻制的印章
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadPersonalSeal",method = RequestMethod.POST)
    public JSONObject uploadPersonalSeal(@RequestParam("file") MultipartFile file){
        return divisionSealService.uploadPersonalSealService(file);
    }

    /**
     * 印章转交
     * @return
     */
    @RequestMapping(value = "/sealForward",method = RequestMethod.POST)
    public JSONObject sealForward(@RequestBody Map<String,String> map){
        System.out.println(map);
        JSONObject jsonObject;
        try{
            TbUser userEntity = ShiroUtils.getUserEntity();
            jsonObject = divisionSealService.sealForwardService(map,userEntity);
        }catch(Exception e){
            CommonResultOutput commonResultOutput = new CommonResultOutput(999,"印章转交执行出现异常");
            jsonObject = JsonUtil.toJSONObject(commonResultOutput);
        }
        return jsonObject;
    }
}
